import os
from teacher import Teacher
from models.student import Student

def clear_screen():
    os.system('cls' if os.name == 'nt' else 'clear')

def teacher_menu():
    teacher = Teacher()
    while True:
        clear_screen()
        print("\n" + "="*20 + " 教师系统 " + "="*20)
        print("1. 生成新试卷")
        print("2. 查看所有试卷")
        print("3. 批改学生答卷")
        print("4. 返回主菜单")
        print("="*50)
        
        choice = input("\n请选择操作：")
        
        if choice == '1':
            exam_id = input("\n请输入试卷ID（如: exam1）：")
            try:
                question_count = int(input("请输入题目数量（默认50题）：") or "50")
                teacher.generate_exam(exam_id, question_count)
                print(f"\n试卷 {exam_id} 生成成功！")
                # 显示试卷内容
                exam = teacher.file_handler.load_exam(exam_id)
                print(teacher.format_exam_questions(exam['questions']))
                input("\n按回车继续...")
            except Exception as e:
                print(f"\n生成试卷失败：{str(e)}")
                input("\n按回车继续...")
                
        elif choice == '2':
            exams = teacher.file_handler.list_exams()
            if not exams:
                print("\n当前没有试卷！")
            else:
                print("\n现有试卷：")
                for exam_id in exams:
                    print(f"- {exam_id}")
                
                view_id = input("\n请输入要查看的试卷ID（直接回车返回）：")
                if view_id:
                    exam = teacher.file_handler.load_exam(view_id)
                    if exam:
                        print(teacher.format_exam_questions(exam['questions']))
            input("\n按回车继续...")
            
        elif choice == '3':
            submissions = teacher.file_handler.list_submissions()
            if not submissions:
                print("\n当前没有待批改的答卷！")
            else:
                print("\n待批改的答卷：")
                for sub_id in submissions:
                    print(f"- {sub_id}")
                
                sub_id = input("\n请输入要批改的答卷ID：")
                if sub_id:
                    try:
                        score = teacher.grade_submission(sub_id)
                        print(f"\n批改完成！得分：{score}分")
                    except Exception as e:
                        print(f"\n批改失败：{str(e)}")
            input("\n按回车继续...")
            
        elif choice == '4':
            break

def student_menu():
    name = input("\n请输入你的名字：")
    student = Student(name)
    
    while True:
        clear_screen()
        print("\n" + "="*20 + " 学生系统 " + "="*20)
        print(f"姓名：{name}")
        print("-"*50)
        print("1. 查看可用试卷")
        print("2. 开始答题")
        print("3. 查看成绩")
        print("4. 查看错题")
        print("5. 返回主菜单")
        print("="*50)
        
        choice = input("\n请选择操作：")
        
        if choice == '1':
            exams = student.list_available_exams()
            if not exams:
                print("\n当前没有可用的试卷！")
            else:
                print("\n可用试卷：")
                for exam_id in exams:
                    print(f"- {exam_id}")
            input("\n按回车继续...")
            
        elif choice == '2':
            exam_id = input("\n请输入要答题的试卷ID：")
            try:
                submission_id = student.take_exam(exam_id)
                print("\n答题完成！")
                print(f"你的答卷ID为：{submission_id}")
                input("\n按回车继续...")
            except Exception as e:
                print(f"\n答题失败：{str(e)}")
                input("\n按回车继续...")
                
        elif choice == '3':
            results = student.view_results()
            if not results:
                print("\n暂无成绩记录！")
            else:
                print("\n你的成绩记录：")
                for result in results:
                    print(f"试卷：{result['exam_id']}")
                    print(f"得分：{result['score']}分")
                    print(f"提交时间：{result['timestamp']}")
                    print("-"*30)
            input("\n按回车继续...")
            
        elif choice == '4':
            exam_id = input("\n请输入要查看错题的试卷ID：")
            try:
                wrong_info = student.view_wrong_questions(exam_id)
                print("\n" + "="*20 + " 错题分析 " + "="*20)
                print(f"试卷：{wrong_info['exam_id']}")
                print(f"得分：{wrong_info['score']}分")
                print(f"题目总数：{wrong_info['total_questions']}")
                print(f"错题数量：{wrong_info['wrong_count']}")
                print(f"提交时间：{wrong_info['submission_time']}")
                
                if wrong_info['wrong_questions']:
                    print("\n错题详情：")
                    print("-"*50)
                    for wrong in wrong_info['wrong_questions']:
                        print(f"第{wrong['number']}题：{wrong['question']}")
                        print(f"你的答案：{wrong['your_answer']}")
                        print(f"正确答案：{wrong['correct_answer']}")
                        print("-"*50)
                else:
                    print("\n恭喜！本次测试没有错题！")
            except Exception as e:
                print(f"\n查看错题失败：{str(e)}")
            input("\n按回车继续...")
            
        elif choice == '5':
            break

def main():
    while True:
        clear_screen()
        print("\n" + "="*20 + " 欢迎使用算术练习系统 " + "="*20)
        print("1. 教师入口")
        print("2. 学生入口")
        print("3. 退出系统")
        print("="*50)
        
        choice = input("\n请选择入口：")
        
        if choice == '1':
            teacher_menu()
        elif choice == '2':
            student_menu()
        elif choice == '3':
            print("\n感谢使用！再见！")
            break

if __name__ == "__main__":
    main() 